package com.dragon.blelibrary.bleutils;

import android.text.format.DateFormat;


import com.dragon.blelibrary.BleConfig;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * Log工具类，具备打印规则控制，打印并保存本地文件等操作
 */
 class LogUtils {

    private static SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
    private static Date mDate = new Date();

    static String PROJECT_OTA = "ota.txt";
    static String PROJECT_MONKEY = "monkey.txt";
    static String PROJECT_DISCONNECT = "connectAndDisconnect.txt";
    static String PROJECT_LOG = "Log.txt";
    public final static String SD_NOTIFICATION_SETTING_LOG_TXT = "notificationSettingLog.txt";
    public final static String SD_NOTIFICATION_PUSH_LOG_TXT = "notificationPushLog.txt";
    public final static String SD_PHONE_CALL_LOG_TXT = "phoneCallLog.txt";
    public final static String SD_SMS_MSG_LOG_TXT = "smsMsgLog.txt";
    public final static String SD_HEART_RATE_LOG_TXT = "heart_rate_log.txt";
    static boolean V_DEBUG = true;
    static boolean I_DEBUG = true;
    static boolean E_DEBUG = true;
    static boolean W_DEBUG = true;

    private static void checkBetaOrLine() {
        if ("0".equals(BleConfig.debugLog)) {
            V_DEBUG = false;
            I_DEBUG = false;
            E_DEBUG = false;
            W_DEBUG = false;
        } else {
            V_DEBUG = true;
            I_DEBUG = true;
            E_DEBUG = true;
            W_DEBUG = true;
        }
    }

    /**
     * 初始化程序log打印规则
     *
     * @param v_log : true 使能 v 等级log ; false 关闭 v 等级log
     * @param i_log : true 使能 i 等级log ; false 关闭 i 等级log
     * @param e_log : true 使能 e 等级log ; false 关闭 e 等级log
     */
    public static void InitLogUtils(boolean w_log, boolean v_log,
                                    boolean i_log, boolean e_log) {
        V_DEBUG = v_log;
        I_DEBUG = i_log;
        E_DEBUG = e_log;
        W_DEBUG = w_log;
    }

    public static void v(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.v(tag, msg);
        }

    }

    public static void v_write(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.v(tag, msg);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "V<" + tag + ">---" + msg);
    }

    public static void v(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.v(tag, msg, t);
        }

    }

    public static void v_write(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.v(tag, msg, t);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "Vt<" + tag + ">---" + msg);

    }

    public static void w(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.w(tag, msg);
        }

    }

    public static void w_write(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.w(tag, msg);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "V<" + tag + ">---" + msg);
    }

    public static void w(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.w(tag, msg, t);
        }

    }

    public static void w_write(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.w(tag, msg, t);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "Vt<" + tag + ">---" + msg);

    }

    public static void i(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.i(tag, msg);
        }

    }

    public static void i_write(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.v(tag, msg);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "V<" + tag + ">---" + msg);
    }

    public static void i(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.i(tag, msg, t);
        }

    }

    public static void i_write(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.i(tag, msg, t);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "Vt<" + tag + ">---" + msg);

    }

    public static void e(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.e(tag, msg);
        }

    }

    public static void e_write(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.e(tag, msg);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "V<" + tag + ">---" + msg);
    }


    public static void e(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.e(tag, msg, t);
        }

    }

    public static void e_write(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.e(tag, msg, t);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "Vt<" + tag + ">---" + msg);

    }

    public static void d(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.d(tag, msg);
        }

    }

    public static void d_write(String tag, String msg) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.d(tag, msg);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "V<" + tag + ">---" + msg);
    }

    public static void d(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.d(tag, msg, t);
        }

    }

    public static void d_write(String tag, String msg, Throwable t) {
        checkBetaOrLine();
        if (V_DEBUG && msg != null) {
            android.util.Log.d(tag, msg, t);
        }
        mDate.setTime(System.currentTimeMillis());
        FileUtils.writeTOfileAndActiveClear(
                PROJECT_LOG, mDateFormat.format(mDate)
                        + "Vt<" + tag + ">---" + msg);

    }

    /**
     * e 转 String
     *
     * @param ex
     * @return
     */
    public static String exToString(Exception ex) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        Throwable cause = ex.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        return result;
    }

    /**
     * APP端 SD 卡 log 记录,暂时只用于通知栏/短信/电话 三个功能做记录
     *
     * @param tag      Log标签
     * @param logMsg   记录信息内容
     * @param fileName 保存文件内容,文件名各模块独立在LogUtils 中定义常量
     */
    public static void writeToSDForMsg(String tag, String logMsg, String fileName) {
        boolean isNeedLog = false;
        checkBetaOrLine();
        if (E_DEBUG) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(
                    DateFormat.format("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis())).append("\t")
                    .append("< ").append(BleConfig.versionName).append(" SDK = ").append(android.os.Build.VERSION.SDK_INT).append(" >")
                    .append("< ").append(tag).append(" >")
                    .append("\t").append(logMsg).append("\n");
            FileUtils.writeTOfileAndActiveClear(fileName, stringBuilder.toString());
            if (isNeedLog)
                android.util.Log.e(tag, stringBuilder.toString());
        }
    }

    /**
     * APP端 SD 卡 log 记录,暂时只用于心率功能做记录
     *
     * @param tag       Log标签
     * @param timeStamp 时间戳
     * @param logMsg    记录信息内容
     */
    public static void writeToSDForHR(String tag, int timeStamp, String logMsg) {
        boolean isNeedLog = false;
        checkBetaOrLine();
        if (E_DEBUG) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(DateFormat.format("yyyy-MM-dd HH:mm:ss", timeStamp)).append("\t").append(logMsg).append("\n");
            FileUtils.writeTOfileAndActiveClear(SD_HEART_RATE_LOG_TXT, stringBuilder.toString());
            if (isNeedLog)
                android.util.Log.e(tag, stringBuilder.toString());
        }
    }

    /***
     * APP端 SD 卡 运动轨迹 log 记录
     * @param tag Log标签
     * @param stringBuilder 定位点信息
     */
    public static void writeToSDForMap(String tag, StringBuilder stringBuilder, String fileName) {
        boolean isNeedLog = false;
        checkBetaOrLine();
        if (E_DEBUG) {
            FileUtils.writeTOfile(fileName, stringBuilder.toString());
            if (isNeedLog)
                android.util.Log.e(tag, stringBuilder.toString());
        }
    }

    public static void writeToSD(String msg, String fileName) {
        checkBetaOrLine();
        if (E_DEBUG) {
            FileUtils.writeToFileInRoot(fileName, msg);
        }
    }

    /**
     * G_sensor数据记录
     *
     * @param tag
     * @param stringBuilder
     * @param fileName      文件名
     */
    public static void writeToSDForGsensor(String tag, StringBuilder stringBuilder, String fileName) {
        boolean isNeedLog = false;
        checkBetaOrLine();
        FileUtils.writeGsensorTOfile(fileName, stringBuilder.toString());
        if (isNeedLog)
            android.util.Log.e(tag, stringBuilder.toString());
    }

    /**
     * 运动数据记录
     *
     * @param tag
     * @param msg
     * @param fileName 文件名
     */
    public static void writeToSDForSport(String tag, String msg, String fileName) {
        boolean isNeedLog = false;
        checkBetaOrLine();
        FileUtils.writeSportTOfile(fileName, msg);
        if (isNeedLog)
            android.util.Log.e(tag, msg);
    }

    /**
     * OTA升级记录数据
     *
     * @param tag
     * @param from
     * @param command
     * @param logMsg
     * @param MsgCodeflag
     */
    public static void writeToSDForOTA(String tag, String from, String command, String logMsg, int MsgCodeflag) {
        checkBetaOrLine();
        if (V_DEBUG) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(DateFormat.format("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis())).append("\t")
                    .append("< ").append(tag).append(" >").append("\t")
                    .append(from).append("\t")
                    .append(command).append("\t")
                    .append(logMsg).append("\t")
                    .append(MsgCodeflag).append("\t").append("\n");
            android.util.Log.e(tag, stringBuilder.toString());
            FileUtils.writeTOfileAndActiveClear(PROJECT_OTA, stringBuilder.toString());
        }
    }

    /**
     * 连接断开压力测试数据数据
     *
     * @param tag
     * @param from
     * @param command
     * @param logMsg
     * @param MsgCodeflag
     */
    public static void writeToSDForConnectAndDisconnect(String tag, String from, String command, String logMsg, int MsgCodeflag) {
        checkBetaOrLine();
        if (V_DEBUG) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(DateFormat.format("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis())).append("\t")
                    .append("< ").append(tag).append(" >").append("\t")
                    .append(from).append("\t")
                    .append(command).append("\t")
                    .append(logMsg).append("\t")
                    .append(MsgCodeflag).append("\t").append("\n");
            android.util.Log.e(tag, stringBuilder.toString());
            FileUtils.writeTOfileAndActiveClear(PROJECT_DISCONNECT, stringBuilder.toString());
        }
    }

    /**
     * monkey测试数据
     *
     * @param tag
     * @param from
     * @param command
     * @param logMsg
     * @param MsgCodeflag
     */
    public static void writeToSDForMonkey(String tag, String from, String command, String logMsg, int MsgCodeflag) {
        checkBetaOrLine();
        if (V_DEBUG) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(DateFormat.format("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis())).append("\t")
                    .append("< ").append(tag).append(" >").append("\t")
                    .append(from).append("\t")
                    .append(command).append("\t")
                    .append(logMsg).append("\t")
                    .append(MsgCodeflag).append("\t").append("\n");
            android.util.Log.e(tag, stringBuilder.toString());
            FileUtils.writeTOfileAndActiveClear(PROJECT_MONKEY, stringBuilder.toString());
        }
    }

}
